package com.study.controller;


import com.study.constant.MessageConstant;
import com.study.constant.RedisMessageConstant;
import com.study.entity.Result;
import com.study.utils.SMSUtils;
import com.study.utils.ValidateCodeUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;


@Controller
@ResponseBody
@RequestMapping(value = "/validateCode")
public class ValidateCodeController {
    public static Logger logger = Logger.getLogger(ValidateCodeController.class);
    @Resource(name = "jedisPool")
    private JedisPool jedisPool;

    @RequestMapping(value = "/send4Order")
    public Result send4Order(String telephone){
        Integer randomCode = ValidateCodeUtils.generateValidateCode(4);
        try{
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,randomCode.toString());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER,5*60,randomCode.toString());
        logger.debug("发送的验证码为："+randomCode);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping(value = "/send4Login")
    public Result send4Login(String telephone){
        Integer loginCode = ValidateCodeUtils.generateValidateCode(6);
        try{
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,loginCode.toString());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN,5*60,loginCode.toString());
        logger.debug("发送的验证码为："+loginCode);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

}
